#include "ScanApi.h"


QString ScanRemoteApi::scanPath;

ScanApiResult::RemoteScanApiResult SyncScanApi::initScanPath(){
    const QString queryStrUrl = ScanRemoteApi::QUERY_SCAN_PATH;

    // manager
    QNetworkAccessManager manager;

    // request
    QNetworkRequest request;
    request.setUrl(queryStrUrl);

    // run
    QNetworkReply *reply = manager.get(request);

    // wait
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();

    // set info
    ScanApiResult::RemoteScanApiResult apiRes{200, ""};
    if(reply->error() == QNetworkReply::NoError){
        QString res = reply->readAll();
        QJsonDocument json = QJsonDocument::fromJson(res.toUtf8());
        QJsonObject jsonObj = json.object();
        ScanRemoteApi::scanPath = jsonObj.value("data").toString();
    }else{
        apiRes.code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        apiRes.errMsg = reply->errorString();
    }

    reply->deleteLater();

    return apiRes;
}


ScanApiResult::RemoteScanApiCacheResult SyncScanApi::getNextCache(){
    const QString queryStrUrl = ScanRemoteApi::QUERY_CACHE;

    // manager
    QNetworkAccessManager manager;

    // request
    QNetworkRequest request;
    request.setUrl(queryStrUrl);

    // run
    QNetworkReply *reply = manager.get(request);

    // wait
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();

    // get fileInfo
    ScanApiResult::RemoteScanApiCacheResult cacheRes{200, "", "", 0};

    if(reply->error() == QNetworkReply::NoError){
        QString res = reply->readAll();

        // get res
        QJsonDocument json = QJsonDocument::fromJson(res.toUtf8());
        QJsonObject jsonObj = json.object();

        // get data
        QString dataStr = jsonObj.value("data").toString();
        QJsonDocument dataJson = QJsonDocument::fromJson(dataStr.toUtf8());
        QJsonArray jsonArray = dataJson.array();

        // array is empty
        if(jsonArray.size() == 0){
            cacheRes.code = -1;
        }else{
            QJsonObject firstObj = jsonArray.first().toObject();
            cacheRes.fileName = firstObj["file"].toString();
            cacheRes.id = firstObj["id"].toInt();
        }
    }else{
        cacheRes.code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        cacheRes.errMsg = reply->errorString();
    }

    reply->deleteLater();
    return cacheRes;

}

ScanApiResult::RemoteScanApiResult SyncScanApi::consumeFileCache(int id){
    const QString queryStrUrl = ScanRemoteApi::CONSUME_CACHE;

    QJsonObject dataJson;
    dataJson.insert("file_id", id);

    QJsonDocument dataDoc;
    dataDoc.setObject(dataJson);

    QByteArray jsonByte = dataDoc.toJson(QJsonDocument::Compact);

    // manager
    QNetworkAccessManager manager;

    // request
    QNetworkRequest request;
    request.setUrl(queryStrUrl);

    // run
    QNetworkReply *reply = manager.put(request, jsonByte);

    // wait
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();

    // res
    ScanApiResult::RemoteScanApiResult cacheRes{200, ""};

    if(reply->error() != QNetworkReply::NoError){
        cacheRes.code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        cacheRes.errMsg = reply->errorString();
    }

    reply->deleteLater();

    return cacheRes;
}
